using System;
using System.Collections.Generic;
using System.Text;
using MULENetworking;

namespace MULEServer
{
    public class ColonyCheck
    {
        public ColonyCheck()
        {

        }

        // returns 0 -- everything is fine
        // returns 1 -- warning condition, food shortage
        // returns 2 -- warning condition, energy shortage
        // returns 3 -- warning condition, food and energy shortage
        // returns 4 -- colony fails, food
        // returns 5 -- colony fails, energy
        // returns 6 -- colony fails, food and energy
        public int check(IPlayer player1, IPlayer player2, IPlayer player3, IPlayer player4, Store store, IMapSquare[,] map)
        {
            int foodStatus = foodCheck(player1, player2, player3, player4, store, map);
            int energyStatus = energyCheck(player1, player2, player3, player4, store, map);

            if(foodStatus == 0 && energyStatus == 0) return 0;
            if(foodStatus == 2 && energyStatus == 2) return 6;
            if(foodStatus == 2) return 4;
            if(energyStatus == 2) return 5;
            if(foodStatus == 1 && energyStatus == 1) return 3;
            if(foodStatus == 1) return 1;
            if(energyStatus == 1) return 2;
            return -1;
        }

        /*
         * foodcheck and energyCheck return:
         * 0 -- everything is hunkydory
         * 1 -- warning
         * 2 -- failure
         */
        private int foodCheck(IPlayer player1, IPlayer player2, IPlayer player3, IPlayer player4, Store store, IMapSquare[,] map)
        {
            Boolean checkFood = false;
            int totalFood = player1.food + player2.food + player3.food + player4.food + store.FoodUnits;

            //determine if there are any food plots.  if not, and totalfood <=0, return 2.
            for(int x = 0; x < 9; x++)
            {
                for (int y = 0; y < 5; y++)
                {
                    if(map[x,y].Mule.Equals("food"))
                    {
                        checkFood = true;
                        break;
                    }
                }
                if(checkFood == true) break;
            }
            if (checkFood == false && totalFood <= 0) return 2;
            //if totalFood <= 0, ret 1, warning condition
            if(totalFood <= 0) return 1;
            return 0; // if none of the above return anything, the colony is doing fine.
        }

        private int energyCheck(IPlayer player1, IPlayer player2, IPlayer player3, IPlayer player4, Store store, IMapSquare[,] map)
        {
            Boolean checkEnergy = false;
            int totalEnergy = player1.energy + player2.energy + player3.energy + player4.energy + store.EnergyUnits;

            //determine if there are any energy plots.  if not, and totalEnergy <=0, return 2.
            for (int x = 0; x < 9; x++)
            {
                for (int y = 0; y < 5; y++)
                {
                    if (map[x,y].Mule.Equals("energy"))
                    {
                        checkEnergy = true;
                        break;
                    }
                }
                if (checkEnergy == true) break;
            }
            if (checkEnergy == false && totalEnergy <= 0) return 2;
            //if totalEnergy <= 0, ret 1, warning condition
            if (totalEnergy <= 0) return 1;
            return 0; // if none of the above return anything, the colony is doing fine.
        }


    }
}
